package c.x.jy.admin.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import com.jy.iot.common.util.file.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;

import c.x.jy.common.utils.R;
import c.x.jy.service.utils.Configuration;

/**
 *  
 */
@Controller
@ResponseBody
@RequestMapping("file")
public class FileController {

	protected Logger log = LoggerFactory.getLogger(getClass());
	 @Autowired
	private Configuration Configuration;
	
	 
    @RequestMapping("/uploadFile")
	public R uploadFile( @RequestParam("file")MultipartFile uploadFile,HttpServletRequest request,HttpServletResponse response){
	  //  UploadFile uploadFile = getFile();
    	   String filename=Configuration.getfileName(uploadFile.getOriginalFilename());
	    log.info("上传文件:"+uploadFile.getOriginalFilename()+" | "+filename);
	    if(Configuration.useOss) {
	        try {
	            OSSClient ossClient = new OSSClient(Configuration.OssEndpoint,
	                    Configuration.OssAccessKeyId, Configuration.OssAccessKeySecret);
	            ossClient.putObject(Configuration.OssBucketName, filename, uploadFile.getInputStream());
	            ossClient.shutdown();
	            JSONObject r1 = new JSONObject();
	            r1.put("result",true);
	            r1.put("url",Configuration.OssFileUrl+filename);
	            r1.put("filename",filename);
	         
	            return  R .put(r1);
	        } catch (Exception e) {
	            log.error("上传阿里云OSS出错！", e);
	          //  renderJson(JSONObject.parse("{ result:false,msg:'上传阿里云OSS出错'}"));
	            return  R.error().put("msg", JSONObject.parse("{ result:false,msg:'上传阿里云OSS出错'}"));
	        }
	    }
	    JSONObject r1 = new JSONObject();
	    r1.put("result",true);
	    r1.put("url", Configuration.STATIC_SERVER_URL+filename);
	    return  R .put(r1);
	}
    @RequestMapping("/simditorUpFile")
    public R simditorUpFile(@RequestParam("file")MultipartFile uploadFile){
	       // UploadFile uploadFile = getFile();
	        log.info("上传文件:"+uploadFile.getOriginalFilename()+" | "+Configuration.getfileName(uploadFile.getOriginalFilename()));
	   //     String filename=Configuration.getfileName(uploadFile.getOriginalFilename());
	        if(Configuration.useOss) {
	            try {
	                OSSClient ossClient = new OSSClient(Configuration.OssEndpoint,
	                        Configuration.OssAccessKeyId, Configuration.OssAccessKeySecret);
	                ossClient.putObject(Configuration.OssBucketName, uploadFile.getName(), uploadFile.getInputStream());
	                ossClient.shutdown();
	                JSONObject r = new JSONObject();
	                r.put("success",true);
	                r.put("file_path",Configuration.OssFileUrl+uploadFile.getName()+"?x-oss-process=image/resize,w_600");
	                r.put("filename",uploadFile.getName());
	                r.put("msg","上传成功！");
	                return  R .put(r);
	              
	            } catch (Exception e) {
	                log.error("上传阿里云OSS出错！", e);
	                //renderJson(JSONObject.parse("{ success:false,msg:'上传阿里云OSS出错'}"));
	                return  R.error().put("msg", JSONObject.parse("{ result:false,msg:'上传阿里云OSS出错'}"));
	              
	            }
	        }
	        JSONObject r = new JSONObject();
	        r.put("success",true);
	        r.put("file_path", Configuration.STATIC_SERVER_URL+uploadFile.getName());
	        r.put("msg","上传成功！");
	        return  R .put(r);
	    }
    @RequestMapping("/simditorUpVedioFile")
	    public R simditorUpVedioFile(@RequestParam("file") MultipartFile uploadFile){
	    //    UploadFile uploadFile = getFile();
    	   String filename=Configuration.getfileName(uploadFile.getOriginalFilename());
				log.info("上传文件:"+uploadFile.getOriginalFilename()+" | "+filename);
			 
	        if(Configuration.useOss) {
	            try {
	                OSSClient ossClient = new OSSClient(Configuration.OssEndpoint,
	                        Configuration.OssAccessKeyId, Configuration.OssAccessKeySecret);
	                ossClient.putObject(Configuration.OssBucketName, filename, uploadFile.getInputStream());
	                ossClient.shutdown();
	                JSONObject r = new JSONObject();
	                r.put("success",true);
	                r.put("file_path",Configuration.OssFileUrl+filename);
	                r.put("filename",filename);
	                r.put("msg","上传成功！");
	                return  R .put(r);
	          
	            } catch (Exception e) {
	                log.error("上传阿里云OSS出错！", e);
	               // renderJson(JSONObject.parse("{ success:false,msg:'上传阿里云OSS出错'}"));
	                return  R.error().put("msg", JSONObject.parse("{ result:false,msg:'上传阿里云OSS出错'}"));
	            }
	        }
	        JSONObject r = new JSONObject();
	        r.put("success",true);
	        r.put("file_path", Configuration.STATIC_SERVER_URL+filename);
	        r.put("msg","上传成功！");
	        return  R .put(r);
	    }
}
